package cn.semcorp.zhuhaizj.services.impl;

import cn.semcorp.zhuhaizj.common.Common;
import cn.semcorp.zhuhaizj.exception.ServiceException;
import cn.semcorp.zhuhaizj.mapper.AccountingMapper;
import cn.semcorp.zhuhaizj.mapper.ForeignMapper;
import cn.semcorp.zhuhaizj.pojo.dto.*;
import cn.semcorp.zhuhaizj.pojo.vo.*;
import cn.semcorp.zhuhaizj.repo.IRedisRepository;
import cn.semcorp.zhuhaizj.restful.ResponseCode;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ForeignServiceImpl {

    @Autowired
    private ForeignMapper mapper;

    /**
     * 导入数据
     * @param list
     */
    public Integer updateLists(List<InsertForeignDTO> list) {
        Pattern pattern = Pattern.compile(Common.regex);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 指定你想要的日期格式
        for (InsertForeignDTO insertAssessDTO : list) { // 日期和绩效扣比都应为纯数字

            if (!pattern.matcher(insertAssessDTO.getDate()).matches()){
                throw new ServiceException(ResponseCode.BAD_REQUEST,"日期格式列出现错误,请提供纯数字格式!");
            }

            // 转换Excel序列号为Java Date对象
            Date dateValue = new Date((long)(Double.parseDouble(insertAssessDTO.getDate()) - 25569) * (24 * 60 * 60 * 1000)); // Excel 1900日期系统需要减去25569
            // 格式化日期
            String formattedDate = sdf.format(dateValue);
            insertAssessDTO.setDate(formattedDate);
        }

        // 分批导入
        List<List<InsertForeignDTO>> partitionedList = Lists.partition(list, 500);
        for (List<InsertForeignDTO> partition : partitionedList) {
            mapper.updateLists(partition);
        }
        return partitionedList.size();
    }

    public List<InsertForeignVO> selectNowMonthList() {
        return mapper.selectNowMonthList();
    }

    public Object doPictureForNew(List<InsertPictureForeignDTO> list) {
        Map<String, List<InsertPictureForeignDTO>> map = new HashMap<>();
        for (InsertPictureForeignDTO insertForeignDTO : list) {
            insertForeignDTO.setX(insertForeignDTO.getDate()+insertForeignDTO.getShift());
            if (!map.containsKey(insertForeignDTO.getWorkshop())){
                // 不存在key
                List<InsertPictureForeignDTO> lists = new ArrayList<>();
                lists.add(insertForeignDTO);
                map.put(insertForeignDTO.getWorkshop(),lists);
            }else{
                map.get(insertForeignDTO.getWorkshop()).add(insertForeignDTO);
            }
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 根据日期升序排序
        map.forEach((k,v)->{
            v.sort(Comparator.comparing(dto -> LocalDate.parse(dto.getDate(), formatter)));
        });
        return map;
    }

    public List<InsertForeignVO> queryByCondition(QueryByConditionDTO dto) {
        return mapper.queryByCondition(dto);
    }

    public void doUpdateTableRow(DoUpdateTableRowDTO dto) {
        mapper.doUpdateTableRow(dto);
    }
}
